package com.example.demo.scheduleTask;


import cn.hutool.core.util.IdUtil;
import com.example.demo.bean.Order;
import com.example.demo.service.OrderService;
import com.example.demo.service.impl.MQServiceImpl;
import com.example.demo.untils.RedisUtil;
import com.sun.org.apache.xpath.internal.operations.Or;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.*;
import java.util.concurrent.TimeUnit;


public class StatisticsTask {

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private MQServiceImpl mqServiceImpl;

    private final int hotAccountValue = 1000;

    //每隔10分钟
    @Scheduled(fixedRate = 1000 * 60 * 10)
    public void statisticsHotAccount(){
        String lockKey = "hotAccountRedisLock";
        //通过UUID和线程ID组合成值
        //todo key

        //在Redis中创建键值对作为锁使用
        if(tryAcquireLock(lockKey)){
            // 获取当前时间戳，精确到分钟
            try {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmm");
                Map map = new HashMap<>();
                //统计过去15分钟的订单数
                long time = System.currentTimeMillis();
                for(int i = 0 ;i < 15;i++){
                    String minuteKey = sdf.format(new Date(time - i * 60 * 1000));
                    Map itemMap = redisUtil.hmget(minuteKey);
                    map.putAll(itemMap);
                }
                for(Object key : map.keySet()){
                    Integer num = (Integer) map.get(key);
                    if(num > hotAccountValue){
                        //记录热点账户
                        redisUtil.set(key.toString(),1,15 * 60 * 1000,TimeUnit.SECONDS);
                    }
                }
            }finally {
                releaseLock(lockKey);
            }
        }
    }




    @Autowired
    private OrderService orderService;

    //每隔60分钟
    @Scheduled(fixedRate = 6 * 1000 * 60 * 10)
    public void checkOrderStatus(){
        String lockKey = "redisLock";
        if(tryAcquireLock(lockKey)){
            // 获取当前时间戳，精确到分钟
            try {
               long countOfOrderService = orderService.count();
               int pageSize = 1000;
               long pageNum = countOfOrderService / pageSize;
               for(int i = 1;i <= pageNum;i++){
                   List<Order> orderList = orderService.getAllWithPagination(i,pageSize);
                   for(int j = 0;j < orderList.size();j++){
                       if(orderList.get(i).getStatus() == 1){
                           mqServiceImpl.asyncSendMsg("order","order","order");
                       }
                   }
               }
            }finally {
                releaseLock(lockKey);
            }
        }
    }

    //尝试获取分布式锁
    public boolean tryAcquireLock(String lockKey){
        return redisUtil.set(lockKey,1,10, TimeUnit.SECONDS);
    }

    public void releaseLock(String lockKey){
        redisUtil.del(lockKey);
    }





}
